package com.ny.controller;

import com.ny.entity.Result;
import com.ny.util.MongoDataInitializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/mongo")
public class MongoDataController {
    
    @Autowired
    private MongoDataInitializer mongoDataInitializer;
    
    /**
     * 清空MongoDB中的所有设备数据
     * URL: http://127.0.0.1:8081/mongo/clear
     * Method: POST
     */
    @PostMapping("/clear")
    public Result clearAllData() {
        try {
            mongoDataInitializer.clearAllData();
            return Result.success("MongoDB数据已清空");
        } catch (Exception e) {
            return Result.error("清空数据失败: " + e.getMessage());
        }
    }
    
    /**
     * 重新初始化MongoDB中的设备数据
     * URL: http://127.0.0.1:8081/mongo/init
     * Method: POST
     */
    @PostMapping("/init")
    public Result initializeData() {
        try {
            mongoDataInitializer.initializeData();
            return Result.success("MongoDB数据初始化完成");
        } catch (Exception e) {
            return Result.error("初始化数据失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取MongoDB中的设备数据统计信息
     * URL: http://127.0.0.1:8081/mongo/stats
     * Method: GET
     */
    @GetMapping("/stats")
    public Result getStats() {
        try {
            long totalDevices = mongoDataInitializer.getCount();
            return Result.success("MongoDB中设备总数: " + totalDevices);
        } catch (Exception e) {
            return Result.error("获取统计信息失败: " + e.getMessage());
        }
    }
}